package edu.udel.mboch.towerdefense;

public class BattleGame extends Game
{
	private static final long serialVersionUID = -1992129915244479490L;
	private BattleTDState state;
	
	public BattleGame(BattleTDState state)
	{
		super();
		this.state = state;
	}
	
	public TDState getState()
	{
		return state;
	}

	public boolean isEnd()
	{
		return state.getPlayer().getLives() <= 0 || state.getPlayer2().getLives() <= 0 || (state.getWaves().getWaves().size() == 0 && state.getGrid().getMobs().size() == 0);
	}
	
	public Player getWinner()
	{
		if(state.getPlayer().getLives() < state.getPlayer2().getLives())
			return state.getPlayer2();
		if(state.getPlayer2().getLives() < state.getPlayer().getLives())
			return state.getPlayer();
		return null;
				
	}

	public void update()
	{
		if(!isEnd())
			for(Actor a : actors)
			{
				TDMove move = a.getNextMove(state);
				if(move != null && move.isValid(state))
					move.make(state);
					if(move instanceof PlayerBuildMove)
						((Player)a).getTowers().add(((PlayerBuildMove)move).getTower());
			}

	}
	
	public void restart()
	{
		Player player1;
		//if(state.getPlayer() instanceof BattleHumanPlayer)
			//player1 = new SingleHumanPlayer(state.getPlayer().getName());
		player1 = new BattleAIPlayer(state.getPlayer().getName());
		Player player2;
		//if(state.getPlayer2() instanceof SingleHumanPlayer)
			//player1 = new SingleHumanPlayer(state.getPlayer().getName());
		player2 = new BattleAIPlayer(state.getPlayer2().getName());
		state = new BattleTDState(Levels.loadFromStringSP(Levels.LEVEL1), player1, player2);
		actors.clear();
		addActor(player1);
		addActor(player2);
		addActor(new TimeActor());
		update();
	}
	
	
	public void startNewHumanGame(TDState state)
	{
	}
}
